package com.mall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mall.product.entity.AttrEntity;
import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.service.AttrService;
import com.mall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.product.dao.AttrAttrgroupRelationDao;
import com.mall.product.entity.AttrAttrgroupRelationEntity;
import com.mall.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {




    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getAttrGroupNameByAttrId(Long attrId) {
        return this.baseMapper.getAttrGroupNameByAttrId(attrId);
    }

    @Override
    public void removeAttrGourpList(List<AttrAttrgroupRelationEntity> attrGroupIdList) {
        QueryWrapper<AttrAttrgroupRelationEntity> qw = Wrappers.query();
        Long groupId = attrGroupIdList.get(0).getAttrGroupId();
        List<Long> attrList = attrGroupIdList.stream().map(AttrAttrgroupRelationEntity::getAttrId
       ).collect(Collectors.toList());
        qw.lambda().in(AttrAttrgroupRelationEntity::getAttrId,attrList).eq(AttrAttrgroupRelationEntity::getAttrGroupId,groupId);
        this.baseMapper.delete(qw);
    }

}